package com.khh.controller;

import com.alibaba.fastjson.JSONObject;
import com.khh.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by webrx on 2017-09-06.
 */
@Controller
public class Jackson {

    @RequestMapping(value="/json") @ResponseBody
    public Object view(){
        String[] m = {"java","php","html","jquery","css","李四四","赵六"};
        return m;
    }


    @RequestMapping("/json2") @ResponseBody
    public User bk(){
        User m = new User();
        m.setUid(11);
        m.setUname("kk");
        m.setUage(22);
        return m;
    }

    @RequestMapping("/json3") @ResponseBody
    public List<User> bks(){
        List<User> bks = new ArrayList<User>();
        User m = new User();
        m.setUid(11);
        m.setUname("kk");
        m.setUage(22);
        bks.add(m);
        User m1 = new User();
        m1.setUid(110);
        m1.setUname("ff");
        m1.setUage(220);
        bks.add(m1);
        return bks;
    }


    //fastjson几乎与spring内置jackson一样区别就是返回值
    @RequestMapping("/fastjson") @ResponseBody
    public String fastjson() throws IOException {

        User m = new User();
        m.setUid(11);
        m.setUname("kk");
        m.setUage(22);
       return JSONObject.toJSONString(m);

    }


    //时钟  tools -> test restful web servlet 点击可测post请求
    @RequestMapping(value="/timer",method = RequestMethod.POST)  @ResponseBody
    public String tt(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

}
